package com.itheima.service.impl;

import com.itheima.constant.MessageConstant;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderInfoMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Result;
import com.itheima.service.OrderInfoService;
import com.itheima.utils.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service
public class OrderInfoServiceImpl implements OrderInfoService {
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Result addOrderInfo(Map<String, Object> map)  {
        System.out.println(map);
        try {
            Date orderDate = DateUtils.parseString2Date((String) map.get("orderDate"));
            int setmealId = Integer.parseInt((String) map.get("setmealId"));

        //创建订单对象
        Order order = new Order();

        order.setOrderDate(orderDate);
        order.setOrderStatus("0");
        order.setOrderType((String) map.get("orderType"));
        order.setSetmealId(setmealId);


        //根据用户的电话判断用户是否已经存在----member表
        Member member = memberMapper.findMemberByTelePhone((String) map.get("telephone"));

        //判断当天的预约人数是否已经满额----orderSetting表
        OrderSetting orderSetting = orderSettingMapper.findOrderSettingByDate(orderDate);
        if (orderSetting.getReservations() < orderSetting.getNumber()){
            //预约未满,可以预约

            //判断用户是会员还是新用户
            if (member == null) {
                //表示是新用户,将新用户的数据插入到member中
                //创建member对象
                //Member memb = new Member();
                Member newMember = new Member();
                newMember.setName((String) map.get("name"));
                newMember.setSex((String) map.get("sex"));
                newMember.setIdCard((String) map.get("idCard"));
                newMember.setPhoneNumber((String) map.get("telephone"));
                newMember.setPassword((String) map.get("telephone"));
                newMember.setRegTime(new Date());

                int row = memberMapper.addMember(newMember);//----member表

                //System.out.println("***********添加的id是"+newMember.getId());
                if (row == 0){
                    System.out.println("添加member失败");
                    //return new Result(false,MessageConstant.ADD_MEMBER_FAIL);
                }else{
                    System.out.println("添加member成功");
                    //return new Result(true,MessageConstant.ADD_MEMBER_SUCCESS);
                }
                order.setMemberId(newMember.getId());
                int i = orderMapper.addOrder(order);
                System.out.println("=================="+order.getId());
                if (i == 0){
                    //订单添加失败
                    return new Result(false,MessageConstant.ADD_ORDER_FAIL);
                }else{
                    //订单添加成功
                    //添加成功,修改orderSetting里面的reservations的数量---ordersetting表
                    orderSettingMapper.updateReservationsByOrderDate(orderDate);
                    return new Result(true,MessageConstant.ADD_ORDER_SUCCESS,order.getId());
                }
            }else{
                //老用户
                //查询订单表,看下该日期,该套餐用户是否已经预约过
                long num = orderInfoMapper.findOrderByDateAndMemberIdAndSetMealId(orderDate,setmealId,member.getId());
                if (num == 0) {
                    //表示没有预约过,可以添加预约订单
                    //int row = orderInfoMapper.addOrder(member.getId(),map.get("orderDate"),map.get("orderType"),map.get("setmealId"));
                    order.setMemberId(member.getId());
                    int row = orderMapper.addOrder(order);
                    //System.out.println("=================="+order.getId());
                    if (row == 0){
                        //订单添加失败
                        return new Result(false,MessageConstant.ADD_ORDER_FAIL);
                    }else{
                        //订单添加成功
                        //添加成功,修改orderSetting里面的reservations的数量---ordersetting表
                        orderSettingMapper.updateReservationsByOrderDate(orderDate);
                        return new Result(true,MessageConstant.ADD_ORDER_SUCCESS,order.getId());
                    }
                }else{
                    //预约过,不能再该日期预约该套餐
                    return new Result(false, MessageConstant.HAS_ORDERED);
                    //throw new RuntimeException("您已经在该日期预约过该套餐");
                }
            }
        }else{
            //预约已经满了,不能再预约
            return new Result(false, MessageConstant.ORDER_FULL);
            //throw new RuntimeException("预约已满,请选择其他日期");
        }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ORDER_FAIL);
        }
    }

    @Override
    public void updateStatus(Map param) {
        String orderId=(String) param.get("out_trade_no");
        orderInfoMapper.updateStatus(orderId);
    }

    @Override
    public int findPayStatus(int id) {
        return   orderInfoMapper.findPayStatus(id);
    }

    @Override
    public void addOrderId(String orderId, String id) {
        orderInfoMapper.addOrderId(orderId,id);
    }

    @Override
    public void cancelOrder(int id) {
        orderInfoMapper.cancelOrder(id);
    }
}
